



/**
 * 画线
 */
import Draw from "./Draw";
import Polygon from '../../entity/type/Polygon'

const DEF_STYLE = {
  material: Cesium.Color.YELLOWGREEN.withAlpha(0.6),
  // fill: true
}

export default class DrawPolygon extends Draw {

  constructor(style) {
    super()
    this._style = {
      ...DEF_STYLE,
      ...style
    }
    this._message = '左键点击开始, 右键点击结束'
  }

  _mountEntity() {

    this._delegate = new Cesium.Entity({
      polygon: {
        ...this._style,
        hierarchy: new Cesium.CallbackProperty(() => {
          if (this._positions.length > 2) {
            return new Cesium.PolygonHierarchy(this._positions)
          } else {
            return null
          }
        }, false)
      }
    })
    this._layer.add(this._delegate)
  }

  _onClick(e) {
    e = this.getMouseInfo(e.position)
    let position = this._clampToGround ? e.surfacePosition : e.position
    if (!position) {
      return false
    }
    let len = this._positions.length
    if (len === 0) {
      this._positions.push(position)
      this.createAnchor(position)
      this._floatingAnchor = this.createAnchor(position)
    }
    this._positions.push(position)
    this.createAnchor(position)
  }

  /**
   * 右键结束
   * @param {*} e 
   */
  _onRightClick(e) {
    e = this.getMouseInfo(e.position)
    this.unbindEvent()
    let polygon = new Polygon(this._positions)
    polygon.setStyle(this._style)
    this._plotEvent.raiseEvent(polygon)
  }

}